<?php

namespace quick\admin\actions;


use quick\admin\annotation\AdminAuth;
use quick\admin\components\Component;
use quick\admin\form\Form;
use quick\admin\http\model\BaseImport;
use quick\admin\http\model\CommonExport;
use quick\admin\http\model\CommonImport;
use quick\admin\http\response\JsonResponse;

/**
 *  导出
 * @AdminAuth(title="导入",auth=true,login=true,menu=false)
 * @package app\admin\quick\actions
 */
class ImportAction extends RowAction
{

    public $name = "导入";
    public $title = "导入";

    /**
     * 导出类型 download直接下载  queue异步列队下载
     * @var string
     */
    private $exportType = 'download';


    /**
     * @var CommonImport
     */
    private $importService;


    /**
     * Action constructor.
     * @param string $name
     * @param array $data
     */
    public function __construct(string $btnName = '导入',string $title = '导入')
    {
        $btnName && $this->name = $btnName;
        $title && $this->title = $title;

        $this->request = app()->request;
        $this->init();
    }


    /**
     * 设置导出类
     * @param CommonExport $export
     * @return $this
     */
    public function setImportService(CommonExport $export)
    {
        $this->importService = $export;
        return $this;
    }


    /**
     * 设置为异步导出类型
     * @return $this
     */
    public function setQueue()
    {
        $this->exportType = 'queue';
        return $this;
    }


    /**
     * 设置为异步导出类型
     * @return $this
     */
    public function setDownload()
    {
        $this->exportType = 'download';
        return $this;
    }

    /**
     * 初始化
     * @return $this
     */
    protected function init()
    {
        $this->dialog();
//        $this->getDisplay()->type("text");
        return $this;
    }





    public function getImportService():CommonImport
    {
        if(empty($this->importService)){
            $export = new BaseImport();
            $this->importService = $export;
        }
        return $this->importService;
    }


    /**
     * @return mixed|JsonResponse
     * @throws \quick\admin\Exceptiona
     */
    public function load()
    {

        if (!$this->handleCanRun($this->request, [])) {
            quick_abort(500, '你无权访问');
        }


        $template = $this->request->get("template");
        if($template){
            $exportService = $this->getImportService();
            $exportService->template($exportService->getTemplateName());
        }

        $form = $this->getForm();

        return $this->response()->success("success",$form);

    }


    protected function getForm(){
        $form = Form::make();
        $form->url($this->storeUrl());

        $form->upload('file','上传文件')->file(['csv','xls','xlsx'])
            ->fileSize(10);
        $form->hideReset();
        $form->appendField(Component::custom('div')->style([
            'height' => '60px',
            'width' => '500px',
            'text-align' => 'right',
            'margin-right' => '100px',
            'margin-btn' => '50px',
        ])->children(Component::action("模板下载")->download($this->loadUrl(['template' => 1]))));
        return $form;
    }






    /**
     * @return mixed|JsonResponse
     * @throws \quick\admin\Exception
     * @throws \think\Exception
     */
    public function store()
    {

        if (!$this->handleCanRun($this->request, [])) {
            quick_abort(500, '你无权访问');
        }
        $importService = $this->getImportService();
        $form = $this->getForm();

        $data = $form->getSubmitData($this->request,3);

        $filePath = app()->getRootPath(). "safefile/".$data['file']['url'];
        $data = $importService->import($filePath);

        return $this->response()->success('导入成功');
    }



}
